﻿Type.registerNamespace("RFBR.KnowledgeBase.Diagram");


RFBR.KnowledgeBase.Diagram = function (settings) {
    if (!settings) {
        settings = {};
    }
    if (!this._events) {
        this._events = [];
    }

    this._events = this._events.concat(["SelectionChanged", "NodeEdited"]);
    this._Width = 600;
    this._Height = 400;
    this._Nodes = null;
    this._Parent = null;      
    settings.Inited = { func: this._init, context: this };    
    this._DomNode = null;
    this._Control = null;
    RFBR.KnowledgeBase.Diagram.initializeBase(this, arguments);
};

var diagramProto = RFBR.KnowledgeBase.Diagram.prototype;

diagramProto._init = function () {
    this._render();
};

diagramProto.setHeight = function (value) {
    if (value) {
        this._Height = value;
        this._DomNode.height = value;
    }
};

diagramProto.setWidth = function (value) {
    if (value) {
        this._Width = value;
        this._DomNode.width = value;
    }
};

var Events = MindFusion.Diagramming.Events;
var ShapeNode = MindFusion.Diagramming.ShapeNode;
var Rect = MindFusion.Drawing.Rect;

diagramProto._render = function () {
    this._DomNode = this._createBlock("canvas", "Diagram");
    this._DomNode.id = "diagram";
   // this._DomNode.onclick = this.draw.bind(this);
    this._DomNode.width = this._Width;
    this._DomNode.height = this._Height;
    if (this._Parent) {
        $(this._DomNode).appendTo(this._Parent);
    }
    this._Control = $create(MindFusion.Diagramming.Diagram, null, null, null, $get("diagram"));
    this.loadNodes();
    
};


diagramProto.loadNodes = function (nodes) {
    if (!nodes) nodes = this._Nodes;
    if (!nodes) return;
    this._loadChilds({ Childs: nodes}, 0, 10);
}

diagramProto._loadChilds = function (parent, level, x, parentNode) {
    for (var i = 0; i < parent.Childs.length; i++) {
        var node = parent.Childs[i];
        //создание ноды
        var root = new ShapeNode(this._Control);
        y = level * 30 + 10;
        root.setBounds(new Rect(x, y, 20, 20));
        //var id = node.Id;
        root.setText(node.Name);
        root.id = node.id;
        root.setBrush("DeepSkyBlue");
        if (node.Childs.length > 0) 
            root.setExpandable(true);
       
        
        this._Control.addItem(root);
        if (parentNode)
            this._Control.getFactory().createDiagramLink(parentNode, root);
        

        if (node.Childs && node.Childs.length > 0) {
            this._loadChilds(node, level+1, x, root);
        }
        x += node.Childs.length >0 ? node.Childs.length * 30 : 30;
    }
};

diagramProto.setHeight = function (value) {
    if (value) {
        this._Height = value;
        this._DomNode.height = value;
    }
};

diagramProto.setWidth = function (value) {
    if (value) {
        this._Width = value;
        this._DomNode.width = value;
    }
};

diagramProto = null;



RFBR.KnowledgeBase.Diagram.registerClass("RFBR.KnowledgeBase.Diagram", RFBR.Object);
